import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flutterabc/BulletComponent.dart';
import 'package:flutterabc/Liveable.dart';

import 'Particleable.dart';

class EnemyComponent extends RectangleComponent
    with Liveable, CollisionCallbacks, Particleable {
  PositionComponent? _tagEntity;

  EnemyComponent({
    position,
    size,
    angle,
    anchor,
    children,
    priority,
    paint,
    paintLayers,
  }) : super(position: position, size: size, anchor: anchor, paint: paint);

  @override
  Future<void> onLoad() async {
    add(RectangleHitbox());
    initPaint(point: 1000);
    return super.onLoad();
  }

  @override
  void onCollisionStart(Set<Vector2> _, PositionComponent other) {
    if (other is BulletComponent) {
      parent?.add(createParticle(other.center));
      //print("EnemyComponent 碰撞other=${other.toString()} center=$center other=${other.center}");
      changePoint(-50);
      other.removeFromParent();
    }
    super.onCollisionStart(_, other);
  }

  void moveTo(PositionComponent tag) {
    this._tagEntity = tag;
  }

  @override
  void update(double dt) {
    if (_tagEntity != null &&
        _tagEntity!.distance(this) > (_tagEntity!.width + width) / 2) {
      var dir = (_tagEntity!.position - position).normalized();
      position.add(dir * dt * 100);
    }
  }
}
